//
//  QRCodeViewController.swift
//  Status
//
//  Created by 花轮 on 15/5/16.
//  Copyright (c) 2015年 花轮. All rights reserved.
//

import UIKit
import AVFoundation

class QRCodeViewController: UIViewController,UITabBarDelegate,AVCaptureMetadataOutputObjectsDelegate {
    // 冲击波的顶部约束
    @IBOutlet weak var topConstraint: NSLayoutConstraint!
   
    @IBOutlet weak var heightConstraint: NSLayoutConstraint!
    @IBOutlet weak var widthContraint: NSLayoutConstraint!
    
    @IBOutlet weak var scanImage: UIImageView!
    @IBOutlet weak var tabbar: UITabBar!
    @IBAction func close(sender: UIBarButtonItem) {
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
            tabbar.selectedItem = tabbar.items![0] as? UITabBarItem
            tabbar.delegate = self
        
        scan()
        }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        poundAnimate()
    }
    
   private  func poundAnimate()
    {
        topConstraint.constant = -heightConstraint.constant
        view.layoutIfNeeded()
        
        UIView.animateWithDuration(2.0)
            {
                UIView.setAnimationRepeatCount(MAXFLOAT)
                self.topConstraint.constant = self.heightConstraint.constant
                self.view.layoutIfNeeded()
        }
    }

    func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem!) {
        heightConstraint.constant = item.tag == 1 ? widthContraint.constant*0.5 : widthContraint.constant
        
        scanImage.layer.removeAllAnimations()
        
        poundAnimate()
    }
    
    // 拍摄会话
    lazy var session :  AVCaptureSession = {
        return AVCaptureSession()
    }()
    
    // 输入设备
    lazy var videoInput: AVCaptureDeviceInput? = {
     
        let device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
        
        if device == nil
        {
            return nil
        }
        
        return AVCaptureDeviceInput(device: device, error: nil)
    }()
    
    // 输出数据
    lazy var dataOutput: AVCaptureMetadataOutput = {
        return AVCaptureMetadataOutput()
    }()
    
    // 预览视图
    lazy var previewLayer: AVCaptureVideoPreviewLayer = {
       let layer = AVCaptureVideoPreviewLayer(session: self.session)
        layer.frame = self.view.bounds
        
        return layer
    }()
    
    private func scan()
    {
        if !session.canAddInput(videoInput){
            println("无法添加输入设备")
            return
        }
        
        if !session.canAddOutput(dataOutput)
        {
            println("无法添加输出设备")
            return
        }
        
        session.addInput(videoInput)
        
        session.addOutput(dataOutput)
        
        dataOutput.metadataObjectTypes = dataOutput.availableMetadataObjectTypes
        dataOutput.setMetadataObjectsDelegate(self, queue: dispatch_get_main_queue())
        
        session.startRunning()
        
        view.layer.insertSublayer(previewLayer, atIndex: 0)
    }
    
    func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!) {
        println(metadataObjects)
    }
}